package center

import (
	"fmt"
	"log"
	"net"
)

type Conninfo struct {
	net.Conn
	data_len int
	data     []byte
}

func (coni *Conninfo) intoroutine() {
	go coni.handle()
}

func (coni *Conninfo) handle() {

	log.Printf("Client %s connected!\n", coni.RemoteAddr().String())
	log.Print("Start TCP Routine!")

	for {
		coni.data = make([]byte, 2048)
		var err error
		coni.data_len, err = coni.Read(coni.data)
		if err != nil {
			log.Print(err)
			break
		}
		log.Printf("Input: %v, Len: %d", coni.data[:coni.data_len], coni.data_len)
		pak := newPacket()
		pak.handle(coni)
	}

	log.Printf("Client %s disconnected!\n", coni.RemoteAddr().String())
	//断开user_id 到socket的对应关系
	user_id := sockettoid[coni]
	if user_id == 0 {
		fmt.Println("user not found!")
		return
	}
	delete(sockettoid, coni)
	delete(idtosocket, user_id)
	exit_scene(user_id)
	//fmt.Println("user socket deleted!")
	coni.Close()
}
